﻿using ASmile.Extend;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace ASmile.Win
{
    public partial class FrmMDI : FrmBase
    {
        public FrmMDI()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 当前已经打开的MDI子窗体
        /// </summary>
        protected readonly Dictionary<Type, FrmBase> FormList = new Dictionary<Type, FrmBase>();
        /// <summary>
        /// 当前已经打开的MDI子窗体,来自Spring
        /// </summary>
        private readonly Dictionary<string, FrmBase> FormNameList = new Dictionary<string, FrmBase>();
        /// <summary>
        /// 获取 dockpanel 已打开的form对象
        /// </summary>
        public T GetTabForm<T>()
            where T : Control
        {
            return FormList[typeof(T)] as T;
        }
        /// <summary>
        /// 打开DockPanel ，如果不存在则创建新的，存在则激活打开
        /// </summary>
        public TForm ShowDockPanelForm<TForm>(DockPanel dockPanel ,string caption = "")
             where TForm : FrmBase, new()
        {
            foreach (FrmBase item in dockPanel.Documents)
            {
                if (item is TForm src)
                {
                    item.Activate();
                    return src;
                }
            }
            TForm frm = new TForm();            
            if (caption.IsNotEmpty()) frm.Text = caption;
            frm.Show(dockPanel);
            return frm;
        }
        /// <summary>
        /// tab选项卡方式显示窗体
        /// </summary>
        protected T ShowTabForm<T>(DockPanel dockPanel, string formText = null)
            where T : Control, new()
        {
            var frm = CreateSingletonForm<T>();
            if (formText.IsNotEmpty())
            {
                frm.Text = formText;
            }
            frm.Show(dockPanel);
            return frm as T;
        }
        /// <summary>
        /// tab选项卡方式显示窗体
        /// </summary>
        protected T ShowTabForm<T>(DockPanel dockPanel, DockState state, string formText = null)
            where T : Control, new()
        {
            var frm = CreateSingletonForm<T>();
            if (formText.IsNotEmpty())
            {
                frm.Text = formText;
            }
            frm.Show(dockPanel, state);
            return frm as T;
        }
        /// <summary>
        /// spring 创建 tab选项卡方式显示窗体
        /// </summary>
        protected void ShowTabForm(string name, DockPanel dockPanel, string formText = null)
        {
            var frm = CreateSingletonForm(name);
            if (!string.IsNullOrWhiteSpace(formText))
            {
                frm.Text = formText;
            }
            frm.Show(dockPanel);
        }
        /// <summary>
        /// 创建一个 DockContent 对象
        /// </summary>
        private FrmBase CreateDockForm(object control, Action FormCloseHandle)
        {
            FrmBase frm;
            if (control is FrmBase)
            {
                frm = control as FrmBase;
            }
            else if (control is UserControl ucControl)
            {
                frm = new FrmBase();
                ucControl.Parent = frm;
                ucControl.Dock = DockStyle.Fill;
                frm.Text = ucControl.Tag?.ToString();
            }
            else
            {
                throw new NotImplementedException("未实现的单例窗体类型");
            }
            frm.FormClosed += new FormClosedEventHandler(delegate (object sender, FormClosedEventArgs e)
            {
                //关闭时从字典列表中删除
                FormCloseHandle();
            });
            return frm;
        }
        /// <summary>
        /// 创建单例窗体
        /// </summary>
        /// <typeparam name="T">DockContent 或者 UcBaseDockControl</typeparam>
        /// <returns>返回一个单例窗体</returns>
        public FrmBase CreateSingletonForm<T>() where T : Control, new()
        {
            Type controlType = typeof(T);
            if (!FormList.ContainsKey(controlType))
            {
                T t = new T();
                var frm = CreateDockForm(t, () => FormList.Remove(controlType));
                FormList.Add(controlType, frm);
                return frm;
            }
            return FormList[controlType];
        }
        /// <summary>
        /// 创建单例窗口（spring需要设置未非单例模式）
        /// </summary>
        /// <param name="name">spring配置的实例名</param>
        /// <param name="id">如果id不为空，则使用id作为单例的判定key，否则使用name</param>
        /// <returns>返回窗体实例</returns>
        public FrmBase CreateSingletonForm(string name, string id = null)
        {
            string key = id.IsEmpty() ? name : id;
            if (!FormNameList.ContainsKey(key))
            {
                var obj = CreateForm(name);
                var frm = CreateDockForm(obj, () => FormNameList.Remove(key));
                FormNameList.Add(key, frm);
                return frm;
            }
            return FormNameList[key];
        }

        private void FrmMDI_Load(object sender, EventArgs e)
        { }
    }
}